const express = require("express");

const router = express.Router();

// 导入生成id的包
const { v4: uuidv4 } = require("uuid");

const orderModel = require("../../model/orderModel");

router.post("/addorder", async (req, res) => {
    let { order_id, order_name, order_price, order_img, order_color, order_size, order_num, order_phone, order_addr, order_user, order_status } = req.body;
    console.log(order_id);
    let id = "";
    if (order_id == undefined) {
        id = uuidv4();
        // console.log("1.0", id);
    }
    if (order_id) {
        id = order_id;
        // console.log("2.0", id);
    }
    let d = new Date();
    let order_create_time = d.toLocaleString();
    console.log(order_create_time);
    await orderModel
        .add_order(id, order_name, order_price, order_img, order_color, order_size, order_num, order_phone, order_addr, order_user, order_status, order_create_time)
        .then((content) => {
            res.send(content);
        })
        .catch((err) => {
            res.send(err);
        });
});

router.get("/myorder", async (req, res) => {
    let { order_status } = req.query;
    await orderModel
        .get_order(order_status)
        .then((content) => {
            res.send({
                code: 200,
                content,
            });
        })
        .catch((error) => {
            res.send(error);
        });
});

router.post("/removeorder", async (req, res) => {
    let { order_id } = req.body;
    await orderModel
        .detele_order(order_id)
        .then((content) => {
            res.send(content);
        })
        .catch((err) => {
            res.send(err);
        });
});

module.exports = router;
